[[...path]].page.tsx 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. import React, { useMemo } from 'react';
  2. import { IUserHasId } from '@growi/core';
  3. import { model as mongooseModel } from 'mongoose';
  4. import {
  5. NextPage, GetServerSideProps, GetServerSidePropsContext,
  6. } from 'next';
  7. import { useTranslation } from 'next-i18next';
  8. import { serverSideTranslations } from 'next-i18next/serverSideTranslations';
  9. import dynamic from 'next/dynamic';
  10. import Head from 'next/head';
  11. import { useRouter } from 'next/router';
  12. import { BasicLayout } from '~/components/Layout/BasicLayout';
  13. import { CrowiRequest } from '~/interfaces/crowi-request';
  14. import { ISidebarConfig } from '~/interfaces/sidebar-config';
  15. import { IUserUISettings } from '~/interfaces/user-ui-settings';
  16. import { UserUISettingsModel } from '~/server/models/user-ui-settings';
  17. import {
  18. useCurrentUser, useIsSearchPage,
  19. useIsSearchServiceConfigured, useIsSearchServiceReachable,
  20. useCsrfToken, useIsSearchScopeChildrenAsDefault,
  21. useRegistrationWhiteList, useShowPageLimitationXL,
  22. } from '~/stores/context';
  23. import {
  24. usePreferDrawerModeByUser, usePreferDrawerModeOnEditByUser, useSidebarCollapsed, useCurrentSidebarContents, useCurrentProductNavWidth,
  25. } from '~/stores/ui';
  26. import loggerFactory from '~/utils/logger';
  27. import {
  28. CommonProps, getNextI18NextConfig, getServerSideCommonProps, useCustomTitle,
  29. } from '../utils/commons';
  30. const logger = loggerFactory('growi:pages:me');
  31. type Props = CommonProps & {
  32. isSearchServiceConfigured: boolean,
  33. isSearchServiceReachable: boolean,
  34. isSearchScopeChildrenAsDefault: boolean,
  35. userUISettings?: IUserUISettings
  36. sidebarConfig: ISidebarConfig,
  37. showPageLimitationXL: number,
  38. // config
  39. registrationWhiteList: string[],
  40. };
  41. const PersonalSettings = dynamic(() => import('~/components/Me/PersonalSettings'), { ssr: false });
  42. // const MyDraftList = dynamic(() => import('~/components/MyDraftList/MyDraftList'), { ssr: false });
  43. const InAppNotificationPage = dynamic(
  44. () => import('~/components/InAppNotification/InAppNotificationPage').then(mod => mod.InAppNotificationPage), { ssr: false },
  45. );
  46. const MePage: NextPage<Props> = (props: Props) => {
  47. const router = useRouter();
  48. const { t } = useTranslation(['translation', 'commons']);
  49. const { path } = router.query;
  50. const pagePathKeys: string[] = Array.isArray(path) ? path : ['personal-settings'];
  51. const mePagesMap = useMemo(() => {
  52. return {
  53. 'personal-settings': {
  54. title: t('User Settings'),
  55. component: <PersonalSettings />,
  56. },
  57. // drafts: {
  58. // title: t('My Drafts'),
  59. // component: <MyDraftList />,
  60. // },
  61. 'all-in-app-notifications': {
  62. title: t('commons:in_app_notification.notification_list'),
  63. component: <InAppNotificationPage />,
  64. },
  65. };
  66. }, [t]);
  67. const getTargetPageToRender = (pagesMap, keys): {title: string, component: JSX.Element} => {
  68. return keys.reduce((pagesMap, key) => {
  69. return pagesMap[key];
  70. }, pagesMap);
  71. };
  72. const targetPage = getTargetPageToRender(mePagesMap, pagePathKeys);
  73. useIsSearchPage(false);
  74. useCurrentUser(props.currentUser ?? null);
  75. useRegistrationWhiteList(props.registrationWhiteList);
  76. useShowPageLimitationXL(props.showPageLimitationXL);
  77. // commons
  78. useCsrfToken(props.csrfToken);
  79. // // UserUISettings
  80. usePreferDrawerModeByUser(props.userUISettings?.preferDrawerModeByUser ?? props.sidebarConfig.isSidebarDrawerMode);
  81. usePreferDrawerModeOnEditByUser(props.userUISettings?.preferDrawerModeOnEditByUser);
  82. useSidebarCollapsed(props.userUISettings?.isSidebarCollapsed ?? props.sidebarConfig.isSidebarClosedAtDockMode);
  83. useCurrentSidebarContents(props.userUISettings?.currentSidebarContents);
  84. useCurrentProductNavWidth(props.userUISettings?.currentProductNavWidth);
  85. // // page
  86. useIsSearchServiceConfigured(props.isSearchServiceConfigured);
  87. useIsSearchServiceReachable(props.isSearchServiceReachable);
  88. useIsSearchScopeChildrenAsDefault(props.isSearchScopeChildrenAsDefault);
  89. const title = useCustomTitle(props, 'GROWI');
  90. return (
  91. <BasicLayout title={useCustomTitle(props, 'GROWI')}>
  92. <Head>
  93. <title>{title}</title>
  94. </Head>
  95. <header className="py-3">
  96. <div className="container-fluid">
  97. <h1 className="title">{ targetPage.title }</h1>
  98. </div>
  99. </header>
  100. <div id="grw-fav-sticky-trigger" className="sticky-top"></div>
  101. <div id="main" className='main'>
  102. <div id="content-main" className="content-main grw-container-convertible">
  103. {targetPage.component}
  104. </div>
  105. </div>
  106. </BasicLayout>
  107. );
  108. };
  109. async function injectUserUISettings(context: GetServerSidePropsContext, props: Props): Promise<void> {
  110. const req = context.req as CrowiRequest<IUserHasId & any>;
  111. const { user } = req;
  112. const UserUISettings = mongooseModel('UserUISettings') as UserUISettingsModel;
  113. const userUISettings = user == null ? null : await UserUISettings.findOne({ user: user._id }).exec();
  114. if (userUISettings != null) {
  115. props.userUISettings = userUISettings.toObject();
  116. }
  117. }
  118. async function injectServerConfigurations(context: GetServerSidePropsContext, props: Props): Promise<void> {
  119. const req: CrowiRequest = context.req as CrowiRequest;
  120. const { crowi } = req;
  121. const {
  122. searchService,
  123. configManager,
  124. } = crowi;
  125. props.isSearchServiceConfigured = searchService.isConfigured;
  126. props.isSearchServiceReachable = searchService.isReachable;
  127. props.isSearchScopeChildrenAsDefault = configManager.getConfig('crowi', 'customize:isSearchScopeChildrenAsDefault');
  128. props.registrationWhiteList = configManager.getConfig('crowi', 'security:registrationWhiteList');
  129. props.showPageLimitationXL = crowi.configManager.getConfig('crowi', 'customize:showPageLimitationXL');
  130. props.sidebarConfig = {
  131. isSidebarDrawerMode: configManager.getConfig('crowi', 'customize:isSidebarDrawerMode'),
  132. isSidebarClosedAtDockMode: configManager.getConfig('crowi', 'customize:isSidebarClosedAtDockMode'),
  133. };
  134. }
  135. // /**
  136. // * for Server Side Translations
  137. // * @param context
  138. // * @param props
  139. // * @param namespacesRequired
  140. // */
  141. async function injectNextI18NextConfigurations(context: GetServerSidePropsContext, props: Props, namespacesRequired?: string[] | undefined): Promise<void> {
  142. // preload all languages because of language lists in user setting
  143. const nextI18NextConfig = await getNextI18NextConfig(serverSideTranslations, context, namespacesRequired, true);
  144. props._nextI18Next = nextI18NextConfig._nextI18Next;
  145. }
  146. export const getServerSideProps: GetServerSideProps = async(context: GetServerSidePropsContext) => {
  147. const req = context.req as CrowiRequest<IUserHasId & any>;
  148. const { user, crowi } = req;
  149. const result = await getServerSideCommonProps(context);
  150. // check for presence
  151. // see: https://github.com/vercel/next.js/issues/19271#issuecomment-730006862
  152. if (!('props' in result)) {
  153. throw new Error('invalid getSSP result');
  154. }
  155. const props: Props = result.props as Props;
  156. if (user != null) {
  157. const User = crowi.model('User');
  158. const userData = await User.findById(req.user.id).populate({ path: 'imageAttachment', select: 'filePathProxied' });
  159. props.currentUser = userData.toObject();
  160. }
  161. await injectUserUISettings(context, props);
  162. await injectServerConfigurations(context, props);
  163. await injectNextI18NextConfigurations(context, props, ['translation', 'admin', 'commons']);
  164. return {
  165. props,
  166. };
  167. };
  168. export default MePage;